﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Seller
{
    public partial class wfReleaseForum : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.SetContrlValue("txtTempUrl", GetFormParam("T_URL"));
                //this.ExecJScript("doBoundContent('" + "/Template/" + GetFormParam("T_URL") + "');");
            }
        }

        private void publishingForum()
        {
            string forumTitle = this.GetParamHtValue("forumTitle");
            string forumInfo = this.GetParamHtValue("forumInfo");
            string publishId = msBsc.SvRef.UserInfo.Operator;
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("desId");
            string custKeyword = this.GetParamHtValue("custKeyword");

            if (string.IsNullOrEmpty(forumTitle))
            {
                this.Alert("标题不能为空");
                return;
            }
            if (string.IsNullOrEmpty(countryId)
                || string.IsNullOrEmpty(cityId))
            {
                this.Alert("请选择地区");
                return;
            }
            if (string.IsNullOrEmpty(custKeyword))
            {
                this.Alert("请输入关键字，让你的推荐更易被查找");
                return;
            }
            if (custKeyword.Length > 125)
            {
                this.Alert("关键字过长，不能超过120个字符");
                return;
            }
            if (string.IsNullOrEmpty(forumInfo))
            {
                this.Alert("发布内容不能为空");
                return;
            }

            if (string.IsNullOrEmpty(publishId))
            {
                this.ExecJScript(@"window.location.href = ""/""");
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizSourcePosts sourcePosts = new AppLogic.DataAlc.CBizSourcePosts())
            {
                string[] ret = sourcePosts.AddSourcePosts(publishId, forumTitle, forumInfo, countryId, cityId, destId, custKeyword);
                if (ret[0] == "OK")
                {
                    this.ExecJScript(@"alert('发布成功');$('#title').val('');
                        $('#forumInfo').val();
                        KindEditor.html('#forumInfo', '');");
                }
                else
                {
                    this.Alert(ret[1]);
                }
            }
        }

        //private void doSaveSet()
        //{
        //    string strHeadHtml = this.GetPostHtValue("TEMPLATE_HTML");
        //    string strVpath = "/Template/" + this.GetPostHtValue("TEMPLATE_URL");//只是文件名，目录固定
        //    if (strHeadHtml != string.Empty && strVpath != string.Empty)
        //    {
        //        string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
        //        System.IO.File.WriteAllText(strFFullPath, strHeadHtml, System.Text.Encoding.UTF8);
        //    }
        //    //保存路径
        //    this.ExecJScript("layer.msg('成功保存模版！',1,{type:1,shade:false});");

        //}

        public override void DoAction()
        {
            if (this.strAction == "published")
            {
                publishingForum();
            }
            //if (this.strAction=="doSaveSet")
            //{
            //    doSaveSet();
            //}
            if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}